import os, wx
import config
from gui import guiHelper, settingsDialogs
from . import adb, constants, ocr
# OCR 配置信息
config.conf.spec[constants.OCR] = {
  "defaultRecognizer": "integer(default=0)",
  "vCodeToClipboard": "boolean(default=false)"
}
# 查询配置信息
config.conf.spec[constants.QUERY] = {
  "viewFund": "string(default='')",
  "viewStock": "string(default='')",
  "area": "string(default='北京,北京,东城')"
}
# 手机设备配置信息
config.conf.spec[constants.ADB] = {
  "device": "string(default='')"
}
# 虚拟菜单配置信息
config.conf.spec[constants.MENU] = {
  "category": "integer(default=0)"
}
# NV 宝盒的设置界面类
class SettingsPanel(settingsDialogs.SettingsPanel):
    title = constants.SUMMARY
    def makeSettings(self, settingsSizer):
        bshSettings = guiHelper.BoxSizerHelper(self, sizer = settingsSizer)
        # 复制验证码到剪贴板的复选框
        self.cbVCodeToClipboard = bshSettings.addItem(wx.CheckBox(self, label = _("保存验证码结果到剪贴板")))
        try:
            self.cbVCodeToClipboard.SetValue(config.conf[constants.OCR]['vCodeToClipboard'])
        except:pass
        # 默认文字识别器的组合框
        self.cDefaultRecognizer = bshSettings.addLabeledControl(_('默认文字识别器'), wx.Choice, choices = ocr.getOcrRecognizerNames ())
        self.cDefaultRecognizer.SetSelection (int(config.conf[constants.OCR]['defaultRecognizer']))
        # 填写基金代码的编辑框
        self.tcFundCodeList = bshSettings.addLabeledControl(_("基金代码，多个用英文逗号','隔开"), wx.TextCtrl)
        self.tcFundCodeList.SetValue(config.conf[constants.QUERY]['viewFund'])
        # 填写股票代码的编辑框
        self.tcStockCodeList = bshSettings.addLabeledControl(_("股票代码，多个用英文逗号','隔开"), wx.TextCtrl)
        self.tcStockCodeList.SetValue(config.conf[constants.QUERY]['viewStock'])
        # 填写天气地区代码的编辑框
        self.tcWeatherArea = bshSettings.addLabeledControl(_("天气查询，依次输入省份、城市、县区名称，使用英文逗号','隔开"), wx.TextCtrl)
        self.tcWeatherArea.SetValue(config.conf[constants.QUERY]['area'])
        # 填写手机设备局域网 IP 的编辑框
        self.tcAdbDevice = bshSettings.addLabeledControl(_("手机设备局域网 IP"), wx.TextCtrl)
        self.tcAdbDevice.SetValue(config.conf[constants.ADB]['device'])
        # 网手机安装nvtts服务端
        self.bInstallNVTTS = bshSettings.addItem(wx.Button(self, label = _("安装 NVTTS 服务端到手机")))
        self.bInstallNVTTS.Bind(wx.EVT_BUTTON, self.onInstallNVTTS)
    def onInstallNVTTS(self, event):
        def startActivity(): # 打开NVTTS页面
            adb.shell('am start -n com.nvda.tts/.MainActivity')
        selfDir = os.path.dirname(__file__)
        adb.installApk(os.path.join(selfDir, '..', '..', 'app-release.apk'), startActivity)
    def onSave(self):
        # 保存配置数据
        config.conf[constants.OCR]['vCodeToClipboard'] = self.cbVCodeToClipboard.IsChecked()
        config.conf[constants.OCR]['defaultRecognizer'] = self.cDefaultRecognizer.GetSelection()
        config.conf[constants.QUERY]['viewFund'] = self.tcFundCodeList.GetValue()
        config.conf[constants.QUERY]['viewStock'] = self.tcStockCodeList.GetValue()
        config.conf[constants.QUERY]['area'] = self.tcWeatherArea.GetValue()
        config.conf[constants.ADB]['device'] = self.tcAdbDevice.GetValue()
# 获取保存的股票代码
def getViewStock():
    return config.conf[constants.QUERY]['viewStock'].split(',')
# 获取保存的基金代码
def getViewFund():
    return config.conf[constants.QUERY]['viewFund'].split(',')
# 获取保存的地址
def getArea():
    return config.conf[constants.QUERY]['area'].split(',')
def setMenuCategory(category):
    config.conf[constants.MENU]['category'] = int(category)
def getMenuCategory():
    return int(config.conf[constants.MENU]['category'])